package com.library.daoImpl;

import com.library.dao.Library_UserDao;
import com.library.util.MysqlConnectUtil;
import com.library.vo.Library_User;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Library_UserDaoImpl implements Library_UserDao {

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;


    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean addUser(Library_User user) {

        boolean isSuccess = true;

        try{
            conn = MysqlConnectUtil.getConn();

            String sql_addUser = "insert into user values(?,?,?,?,?,?,?)";

            ps = conn.prepareStatement(sql_addUser);

            ps.setString(1,user.getId());
            ps.setString(2,user.getUsername());
            ps.setString(3,user.getPassword());
            ps.setString(4,user.getRole());
            ps.setString(5,user.getMajor());
            ps.setString(6,user.getPhoneNum());
            ps.setString(7,"0");

            int addUserCount = ps.executeUpdate();

            if(addUserCount!=1){
                isSuccess = false;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
           MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public Library_User selectUserByUsername(String username) {


        Library_User user = null;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_selectUserByName = "select * from user where username=?";

            ps = conn.prepareStatement(sql_selectUserByName);

            ps.setString(1,username);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                user = new Library_User();
                user.setId(rs.getString("userId"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return user;

    }

    @Override
    public Library_User selectUserByUsernameAndPassword(String username, String password) {

        Library_User user = null;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_selectUserByNameAndPass = "select * from user where username=? and password=? and isDelete=0";

            ps = conn.prepareStatement(sql_selectUserByNameAndPass);

            ps.setString(1,username);
            ps.setString(2,password);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                user = new Library_User();
                user.setId(rs.getString("userId"));
                user.setUsername(rs.getString("userName"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setMajor(rs.getString("major"));
                user.setPhoneNum(rs.getString("phoneNum"));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return user;
    }

    @Override
    public boolean modifyUser(Library_User user) {

        boolean isSuccess = true;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_updateUser = "update user set username=?,password=?,major=?,phoneNum=? where userId=? and isDelete=0";

            ps = conn.prepareStatement(sql_updateUser);

            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getMajor());
            ps.setString(4,user.getPhoneNum());
            ps.setString(5,user.getId());

            int updateCount = ps.executeUpdate();

            if(updateCount != 1){
                isSuccess = false;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public boolean deleteUser_reserve(String id) {

        boolean isSuccess = true;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "update user set isDelete=1 where userId=? and isDelete=0";

            ps = conn.prepareStatement(sql_deleteUser);

            ps.setString(1,id);

            int deleteCount = ps.executeUpdate();

            if(deleteCount != 1){
                isSuccess = false;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public boolean deleteUSer_notReserve(String id) {
        boolean isSuccess = true;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "delete from user where userId=? and isDelete=0";

            ps = conn.prepareStatement(sql_deleteUser);

            ps.setString(1,id);

            int deleteCount = ps.executeUpdate();

            if(deleteCount != 1){
                isSuccess = false;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public List<Library_User> selectAllUser() {

        List<Library_User> userList = new ArrayList<>();

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "select * from user where isDelete=0";

            ps = conn.prepareStatement(sql_deleteUser);

            rs = ps.executeQuery();

            while (rs.next()){
                Library_User user = new Library_User();
                user.setUsername(rs.getString("username"));
                user.setMajor(rs.getString("major"));
                user.setPhoneNum(rs.getString("phoneNum"));
                userList.add(user);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return userList;
    }

    @Override
    public List<Library_User> selectUserByCondition(String column, String value) {

        List<Library_User> userList = new ArrayList<>();

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "select * from user where isDelete=0 and "+column+" =?";

            ps = conn.prepareStatement(sql_deleteUser);
            ps.setString(1,value);

            rs = ps.executeQuery();

            while (rs.next()){
                Library_User user = new Library_User();
                user.setUsername(rs.getString("username"));
                user.setMajor(rs.getString("major"));
                user.setPhoneNum(rs.getString("phoneNum"));
                userList.add(user);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return userList;
    }
}
